import {
    roleList,
    roleAdd,
    roleUpdate,
    roleDelete,
    rolePermissions,
    roleAddPermissions,
    roleRemovePermissions
} from "@/api/role";
import { mapRoleListForAssign, mapRoleListForUnassign } from "@/utils/mapRoleList";

export const roleStore = {
    namespaced: true,
    state:{
        //  所有角色的信息
        roleList:[],
        //  映射后的角色列表（用户分配角色）
        roleListForAssign: [],
        //  映射后的角色列表（移除用户分配角色）
        roleListForUnassign:[],
        //  当前行角色的id
        rowRoleId:0,
        //  当前行角色的描述
        rowRoleDes:""
    },
    mutations:{
        setRoleList(state, value){
            state.roleList = value;
        },
        setRoleListForAssign(state, value){
            state.roleListForAssign = value;
        },
        setRoleListForUnassign(state, value){
            state.roleListForUnassign = value;
        },
        setRowRoleId(state, value){
            state.rowRoleId = value;
        },
        setRowRoleDes(state, value){
            state.rowRoleDes = value;
        }
    },
    actions:{
        //  获取所有角色信息
        async roleList(context, data){
            const { limit, page, name} = data;
            try{
                const result = await roleList(limit, page, name);
                if(result.code === 200){
                    context.commit("setRoleList", result.data);
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "获取数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  映射得到用于用户分配角色的数组
        async getRoleListForAssign(context, data){
            try {
                const [allRoles, userRoleIds] = data;
                // const roleList = allRoles.map(role => ({
                //     ...role,
                //     isChecked: userRoleIds.includes(role.id),
                //     isDisabled: role.status === "禁用"
                // }));
                const roleList = mapRoleListForAssign(allRoles,userRoleIds);
                context.commit("setRoleListForAssign", roleList);
                return roleList;
            } catch (error) {
                return Promise.reject("获取数据失败");
            }
        },
        //  映射得到用于移除用户分配角色的数组
        async getRoleListForUnassign(context,data){
            try{
                const {description, id} = data;
                // const roleList = id.map((item,index) => ({
                //     id: id[index],
                //     description: description[index]
                // }))
                const roleList = mapRoleListForUnassign(description, id);
                context.commit('setRoleListForUnassign',roleList);
            }catch(error){
                return Promise.reject("获取数据失败");
            }
        },
        //  添加用户
        async roleAdd(context, data){
            try{
                const result = await roleAdd(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "添加数据失败");
                }
            } catch (error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  更新用户
        async roleUpdate(context, data){
            try{
                const result = await roleUpdate(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "更新数据失败");
                }
            } catch (error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  删除用户
        async roleDelete(context, data){
            try{
                const { id } = data;
                const result = await roleDelete(id);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "删除数据失败");
                }
            } catch (error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  根据角色id获取角色权限信息
        async rolePermissions(context, data){
            try {
                const { role_id } = data;
                const result = await rolePermissions(role_id);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "获取数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        //  给角色分配权限
        async roleAddPermissions(context, data){
            try{
                const result = await roleAddPermissions(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "权限分配失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
         //  移除角色的权限
        async roleDeletePermissions(context, data){
            try{
                const result = await roleRemovePermissions(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "权限分配失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        }

    },
    getters:{
        allRoles: (state) => state.roleList,
        rowRoleId: (state) => state.rowRoleId,
        rowRoleDes: (state) => state.rowRoleDes,
    }
}